<template>
  <base-breadcrumb>
    <a-row :gutter="10" class="page-height">
      <!-- 树搜索 -->
      <a-col :span="6" class="permission-collapsed" :class="{ 'permission-tree-collapsed': collapsed }">
        <a-card :style="{ border: collapsed ? 'none' : 'border: 1px solid #e8e8e8' }" class="page-height-tree">
          <title-name title="组织架构" />
          <PermissionTreeOrganization :collapsed.sync="collapsed" @select="handleSelect" @init="init" :threeLevelAuthority="true"/>
        </a-card>
      </a-col>
      <!-- 树搜索 -->
      <a-col :span="18" class="permission-collapsed page-height-list-wrapper" :style="collapsed ? 'width:calc(100% - 10px)' : ''">
        <searchData :search-data="searchArr" @search="search" />
        <a-card :bordered="false" class="page-height-list">
          <list-table
            ref="table"
            :request="loadData"
            :columns="columns"
            :exportPermission="{ select: 'BIM_006_exp_05', all: 'BIM_006_exp_05' }"
            :exportConfig="{ use: true, select: $route.meta.title, all: $route.meta.title,monitorColums:true}"
          >
            <template>
              <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
            </template>
          </list-table></a-card
        >
      </a-col>
    </a-row>
  </base-breadcrumb>
</template>
<script>
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
import { columnsOne, columnsTwo } from './model'
import { benefitPage } from '@/api/project/optimization'
export default {
  name: 'a' + Date.now(),
  components: {
    PermissionTreeOrganization
  },
  data() {
    return {
      searchArr: [
        // 搜索框
        {
          name: '时间',
          type: 'monthRange',
          value: 'mealMonthTime',
          attr: {
            rangeFormat: ['beginDate', 'endDate']
          }
        }
      ],
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, { query: this.queryParams })
        if (this.queryParams.projectOrgId) {
          return benefitPage(requestParameters).then((res) => {
            res.data.forEach((i) => {
              for(const prop in i) {
                if(!i[prop]) {
                  i[prop] = 0
                }
              }
              // 设计立项效益率
              i.planProjectBenefitRate = (i.planProjectBenefitRate * 100).toFixed(2)
              //双优化立项效益率
              i.doubleOptimizeProjectBenefitRate = (i.doubleOptimizeProjectBenefitRate * 100).toFixed(2)
              //设计实施效益率
              i.planImplBenefitRate = (i.planImplBenefitRate * 100).toFixed(2)
              //双优化实施效益率
              i.doubleOptimizeImplBenefitRate = (i.doubleOptimizeImplBenefitRate * 100).toFixed(2)
              //设计立项效益额（当期/累计）
              i.planProjectBenefitLinesAll =  `${i.planProjectBenefitLines || 0}/${i.planProjectBenefitLinesCumulative ||  0}`
              //双优化立项额（当期/累计）
              i.doubleOptimizeProjectBeneLinesAll =  `${i.doubleOptimizeProjectBeneLines || 0}/${i.doubleOptimizeProjectBeneLinesCumulative || 0}`
              //设计实施效益额（当期/累计）
              i.planImplBenefitLinesAll =  `${i.planImplBenefitLines ||  0}/${i.planImplBenefitLinesCumulative ||  0}`
              //双优化实施效益额（当期/累计）
              i.doubleOptimizeImplBenefitLinesAll =  `${i.doubleOptimizeImplBenefitLines || 0}/${i.doubleOptimizeImplBenefitLinesCumulative ||  0}`
              //设计实施效益率（当期/累计)
              i.planImplBenefitRateAll =  `${i.planImplBenefitRate}/${(i.planImplBenefitRateCumulative * 100).toFixed(2)}`
              //双优化立项效益额（当期/累计)
              i.doubleOptimizeProjectBeneLinesAll =  `${i.doubleOptimizeProjectBeneLines ||  0}/${i.doubleOptimizeProjectBeneLinesCumulative ||  0}`
              //双优化立项效益率（当期/累计)
              i.doubleOptimizeProjectBenefitRateAll =  `${i.doubleOptimizeProjectBenefitRate}/${(i.doubleOptimizeProjectBenefitRateCumulative* 100).toFixed(2) ||  0.00}`
               //设计立项效益率（当期/累计）
              i.planProjectBenefitRateAll =  `${i.planProjectBenefitRate}/${(i.planProjectBenefitRateCumulative * 100).toFixed(2)}`
              //双优化实施效益率（当期/累计）
              i.doubleOptimizeImplBenefitRateAll =  `${i.doubleOptimizeImplBenefitRate}/${(i.doubleOptimizeImplBenefitRateCumulative * 100).toFixed(2)}`
            })
            if (res.code == 200) {
              return res
            }
          })
        } else {
          return {}
        }
      },
      columns: columnsOne,
      customColumns: columnsOne,
      collapsed: false,
      queryParams: {
        projectOrgId: null
      },
      defaultOrgId: null
    }
  },
  methods: {
    handleSelect(node) {
      if (node.projectId) {
        //项目
        this.columns = columnsTwo
        this.queryParams.projectOrgId = node?.projectId
      } else if(!node.projectId && node.orgName){
        this.columns = columnsOne
        this.queryParams.projectOrgId = node?.orgId
      }
      this.list()
      this.customColumns = this.columns
      console.log(this.customColumns,2233)
    },
    search(val) {
      if (val.beginDate) {
        val.beginDate = `${val.beginDate}-01`
        val.endDate = `${val.endDate}-01`
      }
      this.queryParams = {   ...val,
        ...this.queryParams}
      if (!this.queryParams.projectOrgId) {
        this.queryParams.projectOrgId = this.defaultOrgId
      }
      this.list()
    },
    list(params, callback) {
      this.$refs.table.refresh(true)
    },
    init(data) {
      this.queryParams.projectOrgId = data.orgId
      this.defaultOrgId = data.orgId
      if (data.currentTag == 'PD') {
        //项目
        this.columns = columnsTwo
      } else {
        this.columns = columnsOne
      }
      this.list()
    }
  }
}
</script>
